package com.aayush.libs.location;

import android.content.Context;
import android.location.Criteria;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.util.Log;

class DefaultUserLocationListener implements LocationListener {
	private static final String TAG = "Location";

	LocationManager locationManager;
	Context context;
	Criteria criteria;
	String provider;
	private boolean isTracking;

	public DefaultUserLocationListener(Context context) {
		this.context = context;

		locationManager = (LocationManager) this.context
				.getSystemService(Context.LOCATION_SERVICE);

		// Creating a criteria object to retrieve provider
		criteria = new Criteria();
		// Getting the name of the best provider
		provider = locationManager.getBestProvider(criteria, true);
	}

	public DefaultUserLocationListener(Context context, Criteria criteria) {
		this.context = context;
		this.criteria = criteria;

		locationManager = (LocationManager) context
				.getSystemService(Context.LOCATION_SERVICE);
		// Getting the name of the best provider
		provider = locationManager.getBestProvider(criteria, true);
	}

	public DefaultUserLocationListener(Context context, String provider) {
		this.context = context;
		locationManager = (LocationManager) context
				.getSystemService(Context.LOCATION_SERVICE);

		// Creating a criteria object to retrieve provider
		criteria = new Criteria();
		// Getting the name of the best provider
		this.provider = provider;
	}

	public DefaultUserLocationListener(Context context, Criteria criteria,
			String provider) {
		this.context = context;
		this.provider = provider;
		this.criteria = criteria;
		locationManager = (LocationManager) context
				.getSystemService(Context.LOCATION_SERVICE);

	}

	@Override
	public void onLocationChanged(Location location) {
		Log.i(TAG, "Location Changed! new Location is: " + location);
	}

	@Override
	public void onProviderDisabled(String provider) {
		Log.w(TAG, provider + " got offline");

		if (locationManager.getProviders(true).size() > 0) {
			for (String enabledProvider : locationManager.getProviders(true))
				Log.i(TAG, enabledProvider + " is still online");
		} else {
			isTracking = false;
		}
	}

	@Override
	public void onProviderEnabled(String provider) {
		Log.i(TAG, provider + " is online now");
	}

	@Override
	public void onStatusChanged(String provider, int status, Bundle extras) {
		Log.i(TAG, provider + " status changed to: " + status);
	}

	public boolean isTracking() {
		return isTracking;
	}

	public static Location getCurrentLocation(Context context) {
		LocationManager locationManager = (LocationManager) context
				.getSystemService(Context.LOCATION_SERVICE);

		// Creating a criteria object to retrieve provider
		Criteria criteria = new Criteria();
		criteria.setAccuracy(Criteria.ACCURACY_HIGH);
		criteria.setPowerRequirement(Criteria.POWER_HIGH);
		criteria.setHorizontalAccuracy(Criteria.ACCURACY_HIGH);

		// Getting the name of the best provider
		String provider = locationManager.getBestProvider(criteria, true);
		return locationManager.getLastKnownLocation(provider);
	}
}
